package com.chenxiao.rabbitmqprovider.controller;

import com.chenxiao.rabbitmqprovider.pojo.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: CX
 * @Date: Created in  2023/02/03 13:58
 * @version: v1.0
 * @Description: TODO
 **/
@RestController
@Api("消息推送")
public class SendMessageController {
    @Autowired
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法

    @PostMapping("/sendDirectMessage")
    @ApiModelProperty("发送消息")
    public String sendDirectMessage(@RequestBody Message message) {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        if(message != null){
            messageData = message.getMessage();
        }
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
        return "ok";
    }
    @GetMapping("/getMessage")
    @ApiModelProperty("测试")
    public String getMessage(){
        //
       return rabbitTemplate.getExchange();
    }
}
